{% extends "user/user-base.html" %}

{% block user_js_media %}
    <script type="text/javascript">
        window.show_pp_base = 1;
        window.currently_requesting_pp = false;
        window.load_more_pp = function () {
            if (window.currently_requesting_pp) return;
            window.currently_requesting_pp = true;

            $.get('{{ url('user_pp_ajax', user.user.username) }}', {
                start: window.show_pp_base * 10,
                end: (window.show_pp_base + 1) * 10
            }).done(function (data) {
                var results = $(data['results']);
                $('.pp-table').append(results);
                register_time(results.find('.time-with-rel'));
                window.show_pp_base++;
                if (!data['has_more']) {
                    $("#pp-load-link-wrapper").hide();
                }
                window.currently_requesting_pp = false;
            });
        };
    </script>
{% endblock %}

{% block title_ruler %}{% endblock %}

{% block title_row %}
    {% set tab = 'problems' %}
    {% include "user/user-tabs.html" %}
{% endblock %}

{% block user_content %}
    {% if pp_breakdown %}
        <h3 class="pp-breakdown-header">{{ _('Points breakdown') }}</h3>
        <div id="submissions-table" class="pp-table table">
            {% include "user/pp-table-body.html" %}
        </div>
        {% if pp_has_more %}
            <div id="pp-load-link-wrapper">
                <a id="pp-load-more-link" href="javascript:load_more_pp()">{{ _('Load more...') }}</a>
            </div>
        {% endif %}
    {% else %}
        <i>{{ _('This user has not yet solved any problems.') }}</i>
    {% endif %}

    {% if authored %}
        <div class="user-problem-group">
            <h3 class="unselectable toggle closed">
                <span class="fa fa-chevron-right fa-fw"></span>{{ _('Authored problems') }} ({{ authored|length }})
            </h3>
            <table style="display: none" class="table striped toggled">
                <thead>
                <tr>
                    <th>{{ _('Problem') }}</th>
                    <th>{{ _('Category') }}</th>
                    <th>{{ _('Points') }}</th>
                </tr>
                </thead>
                <tbody>
                {% for problem in authored %}
                    <tr>
                        <td>
                            <a href="{{ url('problem_detail', problem.code) }}">{{ problem.name }}</a>
                        </td>
                        <td>{{ problem.group.full_name }}</td>
                        <td><a href="{{ url('ranked_submissions', problem.code) }}">
                            {{ problem.points|floatformat }}{% if problem.partial %}p{% endif %}
                        </a></td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    {% endif %}

    {% if request.user.is_authenticated and request.user != user.user %}
        <div class="hide-solved-problems"><span>
            <form name="form" action="" method="get">
                <input id="hide_solved" style="vertical-align:middle" onclick="form.submit()" type="checkbox"
                       name="hide_solved"{% if hide_solved %} checked{% endif %} value="1">
                <label style="vertical-align:middle" for="hide_solved">{{ _("Hide problems I've solved") }}</label>
            </form>
        </span></div>
    {% else %}
        <hr>
    {% endif %}

    {% for group in best_submissions %}
        <div class="user-problem-group">
            <h3 class="unselectable toggle closed"><span class="fa fa-chevron-right fa-fw"></span>
                {%- with points_str=group.points|floatformat(-3) -%}
                    {{ _('%(group)s (%(val)s points)', group=group.name, val=points_str) }}
                {%- endwith -%}
            </h3>
            <table style="display: none" class="table striped toggled">
                <thead>
                <tr>
                    <th>{{ _('Problem') }}</th>
                    <th>{{ _('Score') }}</th>
                </tr>
                </thead>
                <tbody>
                {% for entry in group.problems %}
                    <tr>
                        <td>
                            <a href="{{ url('problem_detail', entry.code) }}">{{ entry.name }}</a>
                        </td>
                        <td>
                            <a href="{{ url('user_submissions', entry.code, user.user.username) }}">
                                {%- with points_str=entry.points|floatformat(-3), total_str=entry.total|floatformat(-3) -%}
                                    {{ _('%(points)s / %(total)s', points=points_str, total=total_str) }}
                                {%- endwith -%}
                            </a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    {% endfor %}
{% endblock %}
